import { GameComponent } from "@sapi-game/gameComponent";
import { DirtShopLobbyState } from "../states/lobbyState";
import { Game } from "@sapi-game/main";
import { DimensionIds, EntityTypeIds } from "@sapi-game/utils/vanila-data";
import { Hub, isReferee } from "@/func";
import { Player, world } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";

export class DirtShopLobbyDaemon extends GameComponent<DirtShopLobbyState> {
    protected override onAttach(): void {
        //返回大厅
        this.subscribe(
            Game.events.buttonPush,
            (t) => {
                Hub(t.source as Player);
            },
            {
                dimensionId: DimensionIds.Overworld,
                loc: [1926, -59, 1887],
                sourceType: EntityTypeIds.Player,
            }
        );
        //开始游戏
        this.subscribe(
            Game.events.buttonPush,
            (t) => {
                const player = t.source as Player;
                if (!isReferee(player)) {
                    return player.sendMessage("你不是裁判，无法开始游戏");
                }
                if (!this.state.canStartGame()) {
                    return player.sendMessage("人数不足，无法开始");
                }
                this.state.start();
            },
            {
                dimensionId: DimensionIds.Overworld,
                loc: [1923, -59, 1914],
                sourceType: EntityTypeIds.Player,
            }
        );
    }
}
